import 'package:amap_flutter_example/src/coordinate_converter_page.dart';
import 'package:amap_flutter_example/src/geo_fence_page.dart';
import 'package:amap_flutter_example/src/loaction_page.dart';
import 'package:amap_flutter_example/src/map_view_page.dart';
import 'package:fl_extended/fl_extended.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:amap_flutter/amap_flutter.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(
    MaterialApp(
      navigatorKey: FlExtended().navigatorKey,
      scaffoldMessengerKey: FlExtended().scaffoldMessengerKey,
      debugShowCheckedModeBanner: false,
      theme: ThemeData.light(),
      darkTheme: ThemeData.dark(),
      title: 'FlAMap',
      home: const App(),
    ),
  );
}

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    bool isInit = false;
    return Scaffold(
      appBar: AppBar(title: const Text('高德地图')),
      body: Universal(
        width: double.infinity,
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          ElevatedText(
            onPressed: () async {
              isInit = await AmapFlutter().setAMapKey(
                iosKey: '7c531d629818cbf15409c6f899fd5e5d',
                androidKey: '7c531d629818cbf15409c6f899fd5e5d',
                isAgree: true,
                isContains: true,
                isShow: true,
              );
              var sha1 = await AmapFlutter().getSha1();
              print("sha1 $sha1");
              showToast('高德地图ApiKey设置$isInit');
            },
            text: '设置高德key',
          ),
          ElevatedText(
            onPressed: () async {
              if (!isInit) {
                showToast('请先设置高德key');
                return;
              }
              if (!await getPermission(Permission.location)) {
                showToast('未获取到定位权限');
                return;
              }
              push(const MapViewPage());
            },
            text: '高德地图',
          ),
          ElevatedText(
            onPressed: () {
              if (!isInit) {
                showToast('请先设置高德key');
                return;
              }
              push(const AMapLocationPage());
            },
            text: '高德定位功能',
          ),
          ElevatedText(
            onPressed: () {
              if (!isInit) {
                showToast('请先设置高德key');
                return;
              }
              push(const AMapGeoFencePage());
            },
            text: '高德地理围栏功能',
          ),
          ElevatedText(
            onPressed: () {
              push(const CoordinateConverterPage());
            },
            text: '坐标转换器',
          ),
        ],
      ),
    );
  }
}

class ElevatedText extends StatelessWidget {
  const ElevatedText({super.key, required this.text, required this.onPressed});

  final String text;
  final VoidCallback onPressed;

  @override
  Widget build(BuildContext context) =>
      ElevatedButton(onPressed: onPressed, child: Text(text));
}

Future<bool> getPermission(Permission permission) async {
  final PermissionStatus status = await permission.request();
  if (!status.isGranted) {
    await openAppSettings();
    return await permission.request().isGranted;
  }
  return status.isGranted;
}
